package cn.jhc.teachingquality.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.jhc.teachingquality.R;
import cn.jhc.teachingquality.bean.PracticeLog;
import cn.jhc.teachingquality.rx.RxBus;

/**
 * Created by luyanfei on 16-6-7.
 */
public class ListPracticeLogAdapter extends RecyclerView.Adapter<ListPracticeLogAdapter.ViewHolder>{
    private List<PracticeLog> practiceLogs;

    public ListPracticeLogAdapter(List<PracticeLog> practiceLogs) {
        this.practiceLogs = practiceLogs;
    }

    public void addPracticeLogs(List<PracticeLog> newList){
        practiceLogs.clear();
        practiceLogs.addAll(newList);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_log_entry, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final PracticeLog practiceLog = practiceLogs.get(position);
        holder.titleView.setText(practiceLog.getTitle());
        holder.subtitleView.setText(practiceLog.getSubtitle());
        holder.practiceLog = practiceLog;
    }

    @Override
    public int getItemCount() {
        return practiceLogs.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView titleView;
        public TextView subtitleView;
        public PracticeLog practiceLog;
        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RxBus.getInstance().post(practiceLog);
                }
            });
            titleView = (TextView)itemView.findViewById(R.id.log_title_view);
            subtitleView = (TextView)itemView.findViewById(R.id.log_subtitle_view);
        }
    }
}
